﻿using System;
using Cms.Domain;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Cms.Tests.Unit.Domain
{

    [TestClass]
    public class ContentTranslationTests
    {

        [TestMethod]
        public void GetPublishedHtmlAndTitle_NoPublishedVersion_EmptyString()
        {
            Locale locale = new Locale { ID = 1 };
            Content c = new Content();
            c.AddVersion(locale, DateTime.Now.Ticks, "some text", "the title", new Administrator());
            Assert.AreEqual("", c.GetTranslation(1).GetPublishedHtml());
            Assert.AreEqual("", c.GetTranslation(1).GetPublishedTitle());
        }


        [TestMethod]
        public void GetPublishedHtmlAndTitle_PublishedVersionExists_HtmlAndTitle()
        {
            Locale locale = new Locale { ID = 1 };
            Content c = new Content();
            c.AddVersion(locale, DateTime.Now.Ticks, "some text", "the title", new Administrator());
            c.AddVersion(locale, DateTime.Now.Ticks, "some text 2", "the title 2", new Administrator());
            c.Translations[1].PublishedVersion = c.GetTranslation(1).Versions[0];
            Assert.AreEqual("some text", c.GetTranslation(1).GetPublishedHtml());
            Assert.AreEqual("the title", c.GetTranslation(1).GetPublishedTitle());
        }


        [TestMethod]
        public void GetHtmlAndTitle_ByInvalidTimestamp_EmptyString()
        {
            Locale locale = new Locale { ID = 1 };
            Content c = new Content();
            long timestamp = DateTime.Now.Ticks;
            c.AddVersion(locale, timestamp, "some text", "the title", new Administrator());
            c.AddVersion(locale, timestamp + 10000000, "some text 2", "the title 2", new Administrator());
            Assert.AreEqual("", c.GetTranslation(1).GetHtml("1000"));
            Assert.AreEqual("", c.GetTranslation(1).GetTitle("1000"));
        }


        [TestMethod]
        public void GetHtmlAndTitle_ByValidTimestamp_HtmlAndTitle()
        {
            Locale locale = new Locale { ID = 1 };
            Content c = new Content();
            long timestamp = DateTime.Now.Ticks;
            c.AddVersion(locale, timestamp, "some text", "the title", new Administrator());
            c.AddVersion(locale, timestamp + 10000000, "some text 2", "the title 2", new Administrator());
            Assert.AreEqual("some text", c.GetTranslation(1).GetHtml(timestamp.ToString()));
            Assert.AreEqual("the title", c.GetTranslation(1).GetTitle(timestamp.ToString()));
        }

        [TestMethod]
        public void GetVersion_ByInvalidTimestamp_Null()
        {
            Locale locale = new Locale { ID = 1 };
            Content c = new Content();
            long timestamp = DateTime.Now.Ticks;
            c.AddVersion(locale, timestamp, "some text", "the title", new Administrator());
            c.AddVersion(locale, timestamp + 10000000, "some text 2", "the title 2", new Administrator());
            Assert.IsNull(c.GetTranslation(1).GetVersion("1000"));
        }


        [TestMethod]
        public void GetVersion_ByValidTimestamp_Version()
        {
            Locale locale = new Locale { ID = 1 };
            Content c = new Content();
            long timestamp = DateTime.Now.Ticks;
            c.AddVersion(locale, timestamp, "some text", "the title", new Administrator());
            c.AddVersion(locale, timestamp + 10000000, "some text 2", "the title 2", new Administrator());
            Assert.IsNotNull(c.GetTranslation(1).GetVersion(timestamp.ToString()));
        }


        [TestMethod]
        public void PublishVersion_WithValidTimestamp_Success()
        {
            Locale locale = new Locale { ID = 1 };
            Content c = new Content();
            long timestamp = DateTime.Now.Ticks;
            c.AddVersion(locale, timestamp, "some text", "the title", new Administrator());
            c.AddVersion(locale, timestamp + 10000000, "some text 2", "the title 2", new Administrator());
            c.GetTranslation(1).PublishVersion(timestamp.ToString());
            Assert.AreSame(c.GetTranslation(1).GetVersion(timestamp.ToString()), c.GetTranslation(1).PublishedVersion);
        }

        [TestMethod]
        public void GetState_NoPublishedVersion_NullReturned()
        {
            ContentTranslation ct = new ContentTranslation();
            Assert.AreEqual("{\"PublishedVersionID\": null, \"FirstPublished\": null, \"LatestPublished\": null}", ct.GetState());
        }

        [TestMethod]
        public void GetState_PublishedVersionIsSet_IdReturned()
        {
            ContentTranslation ct = new ContentTranslation();
            ContentTranslationVersion ctv = new ContentTranslationVersion() { ID = 666 };
            ct.PublishedVersion = ctv;
            Assert.AreEqual("{\"PublishedVersionID\": 666, \"FirstPublished\": null, \"LatestPublished\": null}", ct.GetState());
        }
    }

}
